﻿package org.dm.3d.Actions.object 
{
	import org.dm.3d.Actions.Action;
	import flash.geom.Vector3D;
	import org.dm.3d.geom.MathExtend;
	/**
	 * ...
	 * @author Scott
	 */
	public class Loading extends Action
	{
		private var goalPos:Array;
		private var goalNum:uint;
		private var _init:Boolean = false;
		private var _pre:Boolean = true;
		private var _root:Boolean = false;
		private var _cur:uint = 0;
		private var _speed:uint = 20;
		
		public function Loading() 
		{
			this.isUsed = true;
			goalPos = new Array();
			var points:Vector.<int> = Vector.<int>([19,6,19,15,19,23,19,32,19,41,19,49,19,58,19,67,19,76,19,84,19,93,19,102,19,110,19,119,19,128,19,137,22,142,29,5,31,142,37,5,37,14,37,23,37,31,37,40,37,49,37,57,37,66,37,75,37,83,37,92,37,101,37,110,37,118,37,127,40,142,45,128,48,142,54,128,57,142,63,128,66,142,72,128,74,142,80,128,83,142,89,128,92,142,96,129,96,138,130,70,130,79,131,61,131,87,133,52,133,96,135,45,135,104,139,36,139,112,143,29,143,120,148,127,149,22,149,68,149,75,150,60,150,84,151,92,153,51,154,101,155,16,155,132,156,42,158,108,160,35,162,11,162,116,162,137,166,28,168,122,170,7,170,141,172,23,176,126,178,5,178,143,181,19,184,129,187,3,187,145,189,17,193,130,196,2,196,145,197,18,201,129,204,4,205,144,206,19,210,127,213,5,213,142,214,23,217,123,221,8,221,28,221,138,224,116,226,35,228,134,229,13,229,109,231,42,232,102,234,50,235,18,235,93,235,129,236,59,237,67,237,85,238,76,241,25,241,122,246,32,246,115,250,39,250,107,253,48,253,99,255,56,255,90,256,65,256,73,256,82,296,143,299,134,302,126,305,118,306,142,308,110,310,102,313,94,314,142,316,85,318,132,319,77,321,124,322,69,323,116,325,60,326,107,328,52,329,98,330,44,332,90,333,36,334,82,336,27,337,74,339,19,339,66,342,11,342,57,345,49,346,5,347,41,350,32,353,24,355,5,358,34,361,43,363,51,364,5,366,59,367,12,369,68,370,20,371,76,373,29,374,84,376,37,377,92,379,45,379,101,381,53,382,109,384,62,385,117,387,70,387,126,390,78,390,134,393,86,393,142,395,95,398,103,401,111,402,142,404,119,407,128,409,136,410,142,463,7,463,15,463,24,463,32,463,41,463,50,463,58,463,67,463,76,463,84,463,93,463,102,463,110,463,119,463,128,463,137,466,142,472,6,475,143,480,26,480,35,480,44,480,52,480,61,480,70,480,79,480,87,480,96,480,105,480,113,480,122,481,5,481,129,483,20,483,143,489,4,490,129,492,19,492,144,498,4,498,129,500,18,501,144,507,4,507,129,509,19,510,143,516,5,516,128,518,20,518,142,524,6,524,125,526,22,526,140,532,9,532,122,534,25,535,137,539,117,540,12,541,30,543,133,545,111,547,37,549,16,550,103,550,129,552,44,553,96,554,52,555,21,556,61,556,87,557,70,557,78,557,123,561,27,562,117,567,34,567,109,571,41,571,101,573,50,573,93,575,58,575,84,576,67,576,76,629,12,629,20,629,29,629,38,629,47,629,55,629,64,629,73,629,81,629,90,629,99,629,108,629,116,629,125,629,134,629,142,631,5,638,142,640,5,647,7,647,16,647,24,647,33,647,42,647,50,647,59,647,68,647,76,647,85,647,94,647,102,647,111,647,120,647,129,647,137,647,142,709,11,709,20,709,29,709,38,709,46,709,55,709,64,709,72,709,81,709,90,709,98,709,107,709,116,709,124,709,133,709,142,711,5,717,142,720,5,724,30,725,38,725,47,725,56,725,64,725,142,726,73,726,82,726,91,726,99,726,108,726,117,726,125,726,134,728,33,729,5,731,40,733,13,735,48,738,20,739,55,743,27,744,63,747,35,748,70,752,42,753,78,756,50,758,85,761,57,762,92,766,64,767,100,770,72,771,107,775,79,777,114,780,86,781,122,784,94,785,129,788,102,790,137,792,110,795,9,795,17,795,26,795,35,795,43,795,52,795,61,795,70,795,78,796,87,796,96,796,105,796,117,796,142,797,113,800,5,804,142,809,5,812,11,812,20,812,28,812,37,812,46,812,55,812,63,812,72,812,81,812,89,812,98,812,107,812,115,812,124,812,133,812,142,866,64,866,73,866,81,867,90,868,55,869,98,870,47,872,106,874,39,876,114,879,32,881,121,884,25,884,69,884,78,885,86,886,60,887,95,887,128,888,52,890,103,891,19,891,44,894,133,895,110,896,37,898,14,901,116,902,137,903,30,906,10,908,121,909,141,910,26,914,7,916,125,917,22,918,142,922,6,924,128,926,20,927,143,931,4,933,78,933,129,935,19,935,70,935,85,936,144,940,4,941,129,944,19,944,70,944,85,944,144,948,4,950,129,952,19,952,70,953,85,953,143,957,5,959,127,961,21,961,70,961,85,961,93,961,102,961,111,961,120,961,142,965,7,969,24,970,70,970,139,972,18,974,10,978,137,979,70,979,79,979,87,979,96,979,105,979,114,979,122,979,131]);
			for (var i:uint = 0; i < points.length/2; i++)
			{
				goalPos.push(new Vector3D(points[2 * i]-500,points[2 * i + 1]-20,300));
				//goalPos.push(new Vector3D(points[3 * i + 1]*30,-points[3 * i]*30+80, points[3 * i + 2]*30));
			}
			goalNum = goalPos.length;
			//goalPos.sortOn("x");
		}
		public override function update(items:Array,time:Number,num:uint):void 
		{
			_cur++;
			if (_cur < 10)
			{
				if (_pre) preMove(items, num, 1000);				
				return;
			}
			if (_cur<11)
			{
				for (var i:uint = 0; i < num; i++)
				{
					items[i].speed = new Vector3D();
				}
				var tem:Array = new Array();
				for (i = 0; i < num; i++)
				{
					tem.push(items[i].pos);
				}
				tem.sortOn("z", Array.NUMERIC | Array.UNIQUESORT);
				for (i = 0; i < num; i++)
				{
					items[i].pos = tem[i];
				}/**/
			}/*
			for (var j:uint = 0; j < 5; j++)
			{
				//if (j > 0) _cur++;
				if (_cur < goalNum + 10+j) 
				{
					addSpeed(items,_cur-10+j);
				}
				if (_cur >= 20+j && _cur < goalNum + 10+j) 
				{
					items[_cur-20+j].speed = new Vector3D();
				}				
			}*/
			if (_cur < 20) return;
			if (_cur < 21+goalNum/_speed) 
			{
				addSpeed(items,(_cur-20)*_speed);
			}
			if (_cur >= 30  ) 
			{
				/*for (var k:uint = 0; k < _speed; k++)
				{
					items[(_cur-30)*_speed+k].speed = new Vector3D();
				}	*/	
				var kmax:uint = (_cur - 30) * _speed ;
				var addk:uint = _speed;
				if (kmax >goalNum)
				{
					addk =  goalNum + _speed-kmax;
					kmax = goalNum;
				}
				var tspeed:Vector3D;
				var w:Number;
				var h:Number;
				if(kmax<goalNum)
					for (var k:uint = 0; k < addk; k++)
					{
						//items[(_cur - 30) * _speed + k].speed = new Vector3D();
						items[(_cur-30)*_speed+k].opos = items[(_cur-30)*_speed+k].pos.clone();
					}
				for (k=0; k < kmax; k++)
				{
					tspeed = items[k].opos.subtract(items[k].pos);
					items[k].damping = .6;
					tspeed.scaleBy(.5);
					items[k].acceleration = tspeed;
				}	/**/	
			}
		}
		
		public function addSpeed(items:Array,pcur:uint):void 
		{
			var left:uint = pcur + _speed;
			left = left > goalNum?goalNum:left;
			for (var i:uint = pcur; i < left;i++ )
			{
				var damping:Number = items[i].damping;
				damping = (1 - damping) / (1 - Math.pow(damping, 10));
				var tvc:Vector3D = goalPos[i].subtract(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
		}
		public function preMove(items:Array,num:uint,distance:Number):void 
		{
			var tvc:Vector3D;
			var damping:Number;
			damping = .95;
			damping = (1 - damping) / (1 - Math.pow(damping, 20));
			for (var i:uint = 0; i < num; i++)
			{
				items[i].visable = true;
				tvc = MathExtend.pInRound(i, 50, distance);
				//tvc.scaleBy(.1+Math.random());
				tvc.decrementBy(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
			this._pre = false;
		}
		
	}
	
}